/*
 * Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.common.network;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Date of create: Oct 9, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.1009
 */
public class InterruptibleInputStream {

    private Logger log = Logger.getLogger(InterruptibleInputStream.class);
    private final int bufferSize = 8192 * 75;
    private byte[] buf = new byte[bufferSize];
    private final AtomicInteger reading = new AtomicInteger(0);
    private final AtomicInteger lastValue = new AtomicInteger(0);

    public void maybeAvailable(final InputStream is, final OutputStream os,
            long timeout) {

        final Thread reader = new Thread() {

            @Override
            public void run() {
                try {
                    while (true) {
                        reading.incrementAndGet();
                        int length = is.read(buf);
                        if (length < 0) {
                            break;
                        }
                        os.write(buf, 0, length);
                    }
                    os.flush();
                    os.close();
                } catch (IOException ex) {
                    log.error("I/O exception during copy stream!");
                }
            }
        };

        reader.start();

        for (;;) {
            lastValue.set(reading.get());
            try {
                reader.join(timeout);
                synchronized (reading) {
                    if (reader.isAlive()) {
                        if (lastValue.get() == reading.get()) {
                            reader.interrupt();
                            is.close();
                            os.flush();
                            os.close();
                            break;
                        }
                    } else {
                        is.close();
                        os.flush();
                        os.close();
                        break;
                    }
                }
            } catch (IOException ex) {
                log.error("I/O exception thrown during closing input stream");
            } catch (InterruptedException ex) {
                log.error("Something interrupted copy I-> O streams");
            }

        }
    }
}
